{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "b56cced1-c3ff-46a8-aaad-44fb60e7284a",
   "metadata": {},
   "source": [
    "# 第一节、Series和DataFrame数据结构"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "756d2a0b-dfa2-4e40-ba9b-21a89afb435f",
   "metadata": {},
   "source": [
    "pandas是Python的核心数据分析支持库，提供了快速、灵活、明确的数据结构。旨在简单、直观地处理关系型、标记型数据。Pandas是Python进行数据分析的必备高级工具。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bb7d152a-a4be-430c-9711-323a77e201ce",
   "metadata": {},
   "source": [
    "Pandas的主要数据结构是Series（一维数据）与DataFrame（二维数据）。这两种数据结构足以处理金融、统计、社会科学、工程等领域里的大多数案例。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "a8f1f2eb-6095-40bb-ab50-03f087e068c3",
   "metadata": {},
   "source": [
    "**处理数据一般分为几个阶段：**\n",
    "\n",
    "1. 数据整理与清洗\n",
    "2. 数据可视化与制表"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ea5e1121-2174-4af2-aec6-99b189472555",
   "metadata": {},
   "source": [
    "## （1）创建Series"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa3dde5d-fafe-43f1-a68c-e700e078d5c8",
   "metadata": {},
   "source": [
    "用列表生成Series，Pandas默认自动生成整数索引，也可以指定索引。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "546bb395-e3b8-4065-8cfa-ed0ea7bf0896",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d0aae3d9-ca50-4f0d-b9a8-2d34e80b0aa8",
   "metadata": {},
   "outputs": [],
   "source": [
    "lst = [0, 1, 7, 9, np.NaN, None, 1024, 512]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "7ce6cdd5-d5e0-4eba-8490-70867fb39f3c",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 无论是Numpy中的NaN还是Python中的None在Pandas中都是以缺失数据NaN对待\n",
    "s1 = pd.Series(data=lst)\n",
    "s2 = pd.Series(data=lst, index=list('abcdefhi'), dtype='float')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "2e150049-0acb-42db-a5a5-8643118be15e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0       0.0\n",
       "1       1.0\n",
       "2       7.0\n",
       "3       9.0\n",
       "4       NaN\n",
       "5       NaN\n",
       "6    1024.0\n",
       "7     512.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s1  # 默认从0开始的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "68ae289b-7850-4a91-9904-c40fe517dad2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a       0.0\n",
       "b       1.0\n",
       "c       7.0\n",
       "d       9.0\n",
       "e       NaN\n",
       "f       NaN\n",
       "h    1024.0\n",
       "i     512.0\n",
       "dtype: float64"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s2  # 指定的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "9ce9ef69-625d-4dc3-925a-2ec394d1667f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 传入字典创建，key是列索引\n",
    "s3 = pd.Series(\n",
    "    data={\n",
    "        'a': 99,\n",
    "        'b': 137,\n",
    "        'c': 149\n",
    "    },\n",
    "    name='Python_score'   # 指定Series名字\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d53dd53d-2cef-4584-9fd8-56f75cb9db6f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "a     99\n",
       "b    137\n",
       "c    149\n",
       "Name: Python_score, dtype: int64"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s3"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "fb0a59c2-1d27-4157-8ae5-f3b7d204f90d",
   "metadata": {},
   "source": [
    "## （2）创建DataFrame"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bc593931-348c-4284-93f5-579c765720c6",
   "metadata": {},
   "source": [
    "DataFrame是由多种类型的列结构组成的二维标签数据结构，类似Excel或者数据库表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "1a2c1448-1b7c-43b0-b296-713a136dd540",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "2c34b006-2642-43fc-b071-700173e173e5",
   "metadata": {},
   "outputs": [],
   "source": [
    "# index作为行索引，字典中的key作为列索引，创建3*3的DataFrame\n",
    "df1 = pd.DataFrame(\n",
    "    data={\n",
    "        'python': [99, 107, 122],\n",
    "        'math': [111, 137, 88],\n",
    "        'english': [68, 108, 43]\n",
    "    },\n",
    "    index=['张三', '李四', '王五']   # 行索引\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "41cef933-20ba-4369-a786-bfa9fd5ea087",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>python</th>\n",
       "      <th>math</th>\n",
       "      <th>english</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>张三</th>\n",
       "      <td>99</td>\n",
       "      <td>111</td>\n",
       "      <td>68</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>李四</th>\n",
       "      <td>107</td>\n",
       "      <td>137</td>\n",
       "      <td>108</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>王五</th>\n",
       "      <td>122</td>\n",
       "      <td>88</td>\n",
       "      <td>43</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    python  math  english\n",
       "张三      99   111       68\n",
       "李四     107   137      108\n",
       "王五     122    88       43"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "1182132e-ee9d-49d5-93c6-c0b98a852da0",
   "metadata": {},
   "outputs": [],
   "source": [
    "df2 = pd.DataFrame(\n",
    "    data=np.random.randint(0, 151, size=(150, 3)),\n",
    "    index=None,  # 行索引默认\n",
    "    columns=['python', 'math', 'english']\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "27860900-be88-4054-8b39-4ccd41dccf1b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>python</th>\n",
       "      <th>math</th>\n",
       "      <th>english</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>116</td>\n",
       "      <td>24</td>\n",
       "      <td>16</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>78</td>\n",
       "      <td>99</td>\n",
       "      <td>119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>136</td>\n",
       "      <td>29</td>\n",
       "      <td>6</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>144</td>\n",
       "      <td>31</td>\n",
       "      <td>80</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>84</td>\n",
       "      <td>56</td>\n",
       "      <td>95</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>145</th>\n",
       "      <td>25</td>\n",
       "      <td>17</td>\n",
       "      <td>92</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>146</th>\n",
       "      <td>54</td>\n",
       "      <td>147</td>\n",
       "      <td>27</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>147</th>\n",
       "      <td>75</td>\n",
       "      <td>73</td>\n",
       "      <td>120</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>148</th>\n",
       "      <td>101</td>\n",
       "      <td>44</td>\n",
       "      <td>9</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>149</th>\n",
       "      <td>97</td>\n",
       "      <td>96</td>\n",
       "      <td>47</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>150 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "     python  math  english\n",
       "0       116    24       16\n",
       "1        78    99      119\n",
       "2       136    29        6\n",
       "3       144    31       80\n",
       "4        84    56       95\n",
       "..      ...   ...      ...\n",
       "145      25    17       92\n",
       "146      54   147       27\n",
       "147      75    73      120\n",
       "148     101    44        9\n",
       "149      97    96       47\n",
       "\n",
       "[150 rows x 3 columns]"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a5ce6da9-dbf6-464a-af01-f60005f898bb",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
